package cn.bdqn.controller;

import cn.bdqn.bean.result.Result;
import cn.bdqn.bean.result.ResultCode;
import cn.bdqn.bean.result.ResultGenerator;
import cn.bdqn.domain.Menber;
import cn.bdqn.domain.Registration;
import cn.bdqn.service.MenberService;
import cn.bdqn.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 挂号表controller
 */
@RestController
@RequestMapping("/OPC")
public class RegistrationController {

    @Autowired
    private RegistrationService registrationService;

    @Autowired
    private MenberService menberService;

    /**
     * 查询所有的挂号信息
     *
     * @param map 返回的数据集合的封装
     * @return 封装好的结果
     * @throws Exception
     */
    @RequestMapping("/getAll")
    public Result getAllRecipe(Map<String, Object> map) throws Exception {

        try {
            List<Registration> registrations = registrationService.queryAll();
            map.put("registrationList", registrations);
            return ResultGenerator.genSuccessResult(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询有误", ResultCode.INTERNAL_SERVER_ERROR);
        }
    }
    /**
     * 根据挂号名查询挂号信息
     *
     * @param map
     * @param regisName
     * @return
     */
    @RequestMapping("/getByRegisName/{regisName}")
    public Result getByRegisName(Map<String, Object> map, @PathVariable("regisName") String regisName) {
        try {
            List<Registration> registrations = registrationService.queryByRegisName(regisName);
            map.put("registrationList", registrations);
            return ResultGenerator.genSuccessResult(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询有误", ResultCode.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据phone指定查询挂号信息
     * @param map
     * @param phone
     * @return
     */
    @RequestMapping("/getByPhone/{phone}")
    public  Result getByPhone(Map<String, Object> map, @PathVariable("phone") String phone){
        try {
            Registration registrations = registrationService.queryByPhone(phone);
            map.put("registrationList", registrations);
            return ResultGenerator.genSuccessResult(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询有误", ResultCode.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 添加挂号信息
     *
     * @param registration
     * @return
     */
    @RequestMapping("/addRegistration")
    public Result addRegistration(Registration registration) {
        try {
            //判断数据库是否存在重复数据
            int count = registrationService.queryVerifyById(registration.getId());
            //当count为0是添加挂号信息 否则提示已预约,还未前来医院受理
            if (count == 0) {
                registrationService.addRegistration(registration);
                return ResultGenerator.genSuccessResult("添加成功");
            } else {
                return ResultGenerator.genFailResult("已预约,还未前来医院受理");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult("添加失败", ResultCode.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 预约审理通过
     *
     * @param menber
     * @return
     */
    @RequestMapping("/updateRegisStatusAndMenberIdByIdAndAddMenberInfo")
    public Result updateRegisStatusAndMenberIdByIdAndAddMenberInfo(Menber menber) {
        int updateCount;
        int addCount;
        int selectCount;
        try {
            //根据id修改挂号状态
            updateCount = registrationService.updateById(menber.getMenberIdcard());
            if (updateCount == 1) {
                //判断数据库是否有相同的数据
                selectCount = menberService.queryVerifyByMenberIdcard(menber.getMenberIdcard());
                if (selectCount == 0) {
                    //添加病人信息
                    addCount = menberService.addMenber(menber);
                    if (addCount == 1) {
                        //根据id修改病人信息编号
                        registrationService.updateMenberIdById(menber.getMenberIdcard(), menber.getId());
                    } else {
                        return ResultGenerator.genFailResult("添加病人信息失败");
                    }
                } else {
                    return ResultGenerator.genFailResult("卡号未注销");
                }
            } else {
                return ResultGenerator.genFailResult("修改挂号状态失败");
            }
            return ResultGenerator.genSuccessResult("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult("失败", ResultCode.INTERNAL_SERVER_ERROR);
        }
    }


}
